package me.aip.mov.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.aip.mov.controller.vo.LabelVod;
import me.aip.mov.entity.Type;
import me.aip.mov.entity.Vod;
import me.aip.mov.mapper.VodMapper;
import me.aip.mov.service.ITypeService;
import me.aip.mov.service.IVodService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author aip9105
 * @since 2022-01-27
 */
@Service
@CacheConfig(cacheNames = {"vodCache"})
public class VodServiceImpl extends ServiceImpl<VodMapper, Vod> implements IVodService {

    @Autowired
    ITypeService iTypeService;

    @Override
    public Page<Vod> findVodPage(Page page, Vod vod) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("vod_id", "vod_name", "vod_pic", "vod_remarks", "vod_score", "vod_actor", "vod_director", "vod_year",
                "vod_area", "vod_pubdate", "type_id","type_id_1 as typePid", "vod_class", "vod_play_url", "vod_content","vod_play_from");

        if (vod != null) {
            if (vod.getTypeId() != null) {
                queryWrapper.eq("type_id", vod.getTypeId());
            }
            if (vod.getTypePid() != null) {
                queryWrapper.eq( "type_id_1", vod.getTypePid());
            }
            if (StringUtils.isNotBlank(vod.getVodYear())) {
                queryWrapper.eq("vod_year", vod.getVodYear());
            }
            if (StringUtils.isNotBlank(vod.getVodClass())) {
                queryWrapper.like("vod_class", vod.getVodClass());
            }
        }
        queryWrapper.eq("vod_lock",0);
        queryWrapper.orderByDesc("vod_year", "vod_up");
        return this.baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<Vod> findCarousel(Integer typePid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("vod_id", "vod_name", "vod_pic", "vod_remarks", "vod_score", "vod_actor", "vod_director", "vod_year",
                "vod_area", "vod_pubdate", "type_id",  "type_id_1 as typePid", "vod_class", "vod_play_url", "vod_content","vod_play_from");
        if (typePid != null && typePid!=0) {
            queryWrapper.eq( "type_id_1", typePid);
        }
        queryWrapper.eq("vod_lock",0);
        queryWrapper.orderByDesc("vod_year", "vod_up");
        queryWrapper.last("limit 7");
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public Page<Vod> searchVod(Page page, String keyword) {
        LambdaQueryWrapper<Vod> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.select(Vod::getVodId, Vod::getVodName,
                Vod::getVodPic, Vod::getVodActor, Vod::getVodDirector,
                Vod::getVodYear, Vod::getVodPubdate, Vod::getVodArea,
                Vod::getTypePid, Vod::getVodRemarks, Vod::getVodClass,
                Vod::getVodPlayUrl, Vod::getVodContent,
                Vod::getVodScore, Vod::getTypeId,
                Vod::getVodPlayFrom);
        queryWrapper.and(StringUtils.isNotBlank(keyword), t -> t.like(Vod::getVodName, keyword).or().like(Vod::getVodActor, keyword));
        queryWrapper.eq(Vod::getVodLock,0);
        Page<Vod> pageList = this.baseMapper.selectPage(page, queryWrapper);
        Map<Integer, String> collect = iTypeService.findLabels().stream().collect(Collectors.toMap(Type::getTypeId, Type::getTypeName));
        for (Vod record : pageList.getRecords()) {
            if (record.getVodPubdate() != null) {
                record.setVodYear(record.getVodPubdate());
            }
            if (record.getTypePid() != null) {
                record.setTypeName(collect.get(record.getTypePid()));
            }
            // vodList.add(record);
        }
        //pageList.setRecords(vodList);
        return pageList;
    }

    @Override
    @CachePut(cacheNames = "guessVod#600", key = "'guess-'+#keyword")
    public List<Vod> guessVod(String keyword) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("vod_id", "vod_name", "vod_pic", "vod_remarks", "vod_score", "vod_actor", "vod_director", "vod_year",
                "vod_area", "vod_pubdate", "type_id",  "type_id_1 as typePid", "vod_class", "vod_play_url", "vod_content","vod_play_from");
        if (keyword != null) {
            queryWrapper.like("vod_name", keyword);
            queryWrapper.or();
            queryWrapper.like("vod_actor", keyword);
        }
        queryWrapper.eq("vod_lock",0);

        queryWrapper.orderByDesc("vod_year", "vod_up");
        queryWrapper.last("limit 12");
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<Vod> recommendVod(String keyword, Integer typePid, Integer typeId) {
        return this.baseMapper.recommendVod(keyword,typePid,typeId);
    }

    @Override
    @CachePut(cacheNames = "labelVodList#600", key = "#typePid")
    public List<LabelVod> labelVodList(Integer typePid) {
        List<LabelVod> labelVodList = new ArrayList<>();
        List<Type> types = iTypeService.findTypes(typePid);
        for (Type type : types) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.select("vod_id", "vod_name", "vod_pic", "vod_remarks", "vod_score", "vod_actor", "vod_director", "vod_year",
                    "vod_area", "vod_pubdate", "type_id",  "type_id_1 as typePid", "vod_class", "vod_play_url", "vod_content","vod_play_from");
            queryWrapper.orderByDesc("vod_year", "vod_up");
            queryWrapper.last("limit 6");
            if (typePid == 0) {
                queryWrapper.eq( "type_id_1", type.getTypeId());
            } else {
                queryWrapper.eq("type_id", type.getTypeId());
            }
            queryWrapper.eq("vod_lock",0);
            List list = this.baseMapper.selectList(queryWrapper);
            labelVodList.add(LabelVod.builder()
                    .type(type)
                    .vodList(list)
                    .build());
        }
        return labelVodList;
    }

    @Override
    public List<Vod> hotList() {
        List<Vod> list = this.baseMapper.hotList();
        return list;
    }

}
